package com.itheima.zuoye.zy3;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
//        "10001,张无忌,男,2023-07-22 11:11:12,东湖-黄鹤楼#10002,赵敏,女,2023-07-22 09:11:21,黄鹤楼-归元禅寺#10003,周芷若,女,2023-07-22 04:11:21,木兰文化区-东湖#10004，小昭,女,2023-07-22 08:11:21,东湖#10005，灭绝,女,2023-07-22 17:11:21,归元禅寺" ;
        String str = "10001,张无忌,男,2023-07-22 11:11:12,东湖-黄鹤楼#10002,赵敏,女,2023-07-22 09:11:21,黄鹤楼-归元禅寺#10003,周芷若,女,2023-07-22 04:11:21,木兰文化区-东湖#10004,小昭,女,2023-07-22 08:11:21,东湖#10005,灭绝,女,2023-07-22 17:11:21,归元禅寺";
        String[] students = str.split("#");
        List<Student> list = new ArrayList<>();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        for (String student : students) {
            String[] info = student.split(",");
            long id = Long.parseLong(info[0]);
            String name = info[1];
            String sex = info[2];
            LocalDateTime time = LocalDateTime.parse(info[3], fmt);
            String attractions = info[4];
            list.add(new Student(id, name, sex, time, attractions));
        }
//        遍历上面获取的List<Student> 集合，统计里面每个景点选择的次数，并输出 景点名称和选择的次数。
        Map<String, Integer> countMap = new HashMap<>();
        for (Student student : list) {
            String route = student.getAttractions();
            if (route == null || route.trim().isEmpty()) continue;

            // 拆分多个景点
            String[] spots = route.split("-");
            for (String spot : spots) {
                spot = spot.trim();
                countMap.put(spot, countMap.getOrDefault(spot, 0) + 1);
            }
        }

        // 输出结果
        for (Map.Entry<String, Integer> entry : countMap.entrySet()) {
            System.out.println(entry.getKey() + " 被选择 " + entry.getValue() + " 次");
        }
//        请用程序计算出哪个景点想去的人数最多，以及哪些人没有选择这个最多人想去的景点。
        String maxThread = "";
        int maxCount = 0;
        for (Map.Entry<String, Integer> entry : countMap.entrySet()) {
            if (entry.getValue() > maxCount) {
                maxCount = entry.getValue();
                maxThread = entry.getKey();
            }
        }

        System.out.println("最多人想去的景点是：" + maxThread + "（" + maxCount + "人）");

        // 哪些人没有选择这个景点
        System.out.println("没有选择此景点的学生：");
        for (Student student : list) {
            String route = student.getAttractions();
            if (route == null || !route.contains(maxThread)) {
                System.out.println(student.getName());
            }
        }
    }

}

